package cn.stu.common.utils;

import cn.hutool.core.collection.CollUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 集合工具
 *
 * @author HGZS
 * @date 2023/09/08
 */
public class CollUtils {

    /**
     * 唯一列表
     *
     * @param list 列表
     * @return {@link List}<{@link T}>
     */
    public static <T> List<T> uniqueList(List<T> list) {
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        if (list.size() < 5000000L) {
            Set<T> uniqueSet = new LinkedHashSet<>(list);
            return new ArrayList<>(uniqueSet);
        }
        if (list.size() < 10000000L) {
            return list.stream().distinct().collect(Collectors.toList());
        }
        return list.parallelStream().distinct().collect(Collectors.toList());
    }

    /**
     * 字符串转Map
     *
     * @param strIds   id
     * @param strNames name
     * @return {@link Map}<{@link String}, {@link String}>
     */
    public static Map<String, String> stringsToMap(String strIds, String strNames) {
        List<String> ids = Arrays.asList(strIds.split(","));
        List<String> names = Arrays.asList(strNames.split(","));
        Map<String, String> map = new HashMap<>(Math.min(ids.size(), names.size()));
        for (int i = 0; i < Math.min(ids.size(), names.size()); i++) {
            map.put(ids.get(i), names.get(i));
        }
        for (int i = Math.min(ids.size(), names.size()); i < ids.size(); i++) {
            map.put(ids.get(i), null);
        }
        return map;
    }
}
